package com.jsg.sl651.message.parser.model;

import lombok.Getter;

/**
 * 遥测站报警及状态信息
 */
@Getter
public class StationStatus {

    private final byte[] statusMessage; // 状态信息原始字节报文  4ge字节
    private final int[] bitInfo; // 状态字节信息处理
    private final EnumData acChargingStatus; // 交流电充电状态
    private final EnumData batteryVoltageStatus; // 蓄电池电压状态
    private final EnumData waterLevelAlarmStatus; // 水位超限报警状态
    private final EnumData flowRateAlarmStatus; // 流量超限报警状态
    private final EnumData waterQualityAlarmStatus; // 水质超限报警状态
    private final EnumData flowMeterStatus; // 流量仪表状态
    private final EnumData waterLevelMeterStatus; // 水位仪表状态
    private final EnumData terminalBoxDoorStatus; // 终端箱门状态
    private final EnumData memoryStatus; // 存储器状态
    private final EnumData icCardFunctionStatus; // IC开功能有效
    private final EnumData pumpWorkingStatus; // 水泵工作状态
    private final EnumData remainingWaterAlarm; // 剩余水量报警


    public StationStatus(byte[] statusMessage) {
        if (statusMessage.length != 4) {
            throw new RuntimeException("数据长度不对,数据长度必须为 4 字节,当前长度： " + statusMessage.length);
        }
        this.statusMessage = statusMessage;
        this.bitInfo = new int[32]; // 4 bytes * 8 bits per byte
        for (int i = 0; i < statusMessage.length; i++) {
            for (int j = 0; j < 8; j++) {
                bitInfo[i * 8 + j] = (statusMessage[i] >> j) & 1;
            }
        }
        this.acChargingStatus = createData(bitInfo[0], "正常", "停电");
        this.batteryVoltageStatus = createData(bitInfo[1], "正常", "电压低");
        this.waterLevelAlarmStatus = createData(bitInfo[2], "正常", "报警");
        this.flowRateAlarmStatus = createData(bitInfo[3], "正常", "报警");
        this.waterQualityAlarmStatus = createData(bitInfo[4], "正常", "报警");
        this.flowMeterStatus = createData(bitInfo[5], "正常", "故障");
        this.waterLevelMeterStatus = createData(bitInfo[6], "正常", "故障");
        this.terminalBoxDoorStatus = createData(bitInfo[7], "开启", "关闭");
        this.memoryStatus = createData(bitInfo[8], "正常", "异常");
        this.icCardFunctionStatus = createData(bitInfo[9], "关闭", "IC卡有效");
        this.pumpWorkingStatus = createData(bitInfo[10], "水泵工作", "水泵停机");
        this.remainingWaterAlarm = createData(bitInfo[11], "未超限", "超限");
    }


    public static EnumData createData(int bit, String zeroStr, String oneStr) {
        return new EnumData(bit, bit == 0 ? zeroStr : oneStr);
    }
}
